{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[ 9,  8,  7],\n",
       "        [ 7,  6,  6],\n",
       "        [10,  7,  8],\n",
       "        [ 8,  4,  5],\n",
       "        [ 9,  9,  3],\n",
       "        [ 8,  6,  7],\n",
       "        [ 7,  5,  6]]), array([[8, 4, 4],\n",
       "        [3, 6, 6],\n",
       "        [6, 3, 3],\n",
       "        [6, 4, 5],\n",
       "        [8, 2, 2]]))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.asarray([[9,8,7],[7,6,6],[10,7,8],[8,4,5],[9,9,3],[8,6,7],[7,5,6]])\n",
    "b = np.asarray([[8,4,4],[3,6,6],[6,3,3],[6,4,5],[8,2,2]])\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.28571429, 6.42857143, 6.        ])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_mean = np.mean(a,axis=0)\n",
    "a_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.2, 3.8, 4. ])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_mean = np.mean(b,axis=0)\n",
    "b_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.71428571,  1.57142857,  1.        ],\n",
       "       [-1.28571429, -0.42857143,  0.        ],\n",
       "       [ 1.71428571,  0.57142857,  2.        ],\n",
       "       [-0.28571429, -2.42857143, -1.        ],\n",
       "       [ 0.71428571,  2.57142857, -3.        ],\n",
       "       [-0.28571429, -0.42857143,  1.        ],\n",
       "       [-1.28571429, -1.42857143,  0.        ]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算离差矩\n",
    "A = a-a_mean\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.8,  0.2,  0. ],\n",
       "       [-3.2,  2.2,  2. ],\n",
       "       [-0.2, -0.8, -1. ],\n",
       "       [-0.2,  0.2,  1. ],\n",
       "       [ 1.8, -1.8, -2. ]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = b-b_mean\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.42857143,  7.14285714,  2.        ],\n",
       "       [ 7.14285714, 17.71428571, -3.        ],\n",
       "       [ 2.        , -3.        , 16.        ]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S_a = A.T.dot(A)\n",
    "S_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 16.8,  -9.8, -10. ],\n",
       "       [ -9.8,   8.8,   9. ],\n",
       "       [-10. ,   9. ,  10. ]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S_b = B.T.dot(B)\n",
    "S_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[24.22857143, -2.65714286, -8.        ],\n",
       "       [-2.65714286, 26.51428571,  6.        ],\n",
       "       [-8.        ,  6.        , 26.        ]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S=S_a+S_b\n",
    "S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.12745741, 0.09034737, 0.09529135])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.linalg.inv(S).dot(a_mean-b_mean)\n",
    "c # 判别系数，最优解由拉格朗日方法得出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 得出判别函数y = cx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.2086282645487754"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求出判别临界值\n",
    "# 购买组的平均值对应的判别值：\n",
    "y_a = np.sum(c*a_mean)\n",
    "y_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5147213226864054"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 非购买组的平均值对应的判别值为：\n",
    "y_b = np.sum(c*b_mean)\n",
    "y_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "y0 = (len(a)*y_a+len(b)*y_b )/ (len(a)+len(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.9195003721061212"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 9,  8,  7],\n",
       "       [ 7,  6,  6],\n",
       "       [10,  7,  8],\n",
       "       [ 8,  4,  5],\n",
       "       [ 9,  9,  3],\n",
       "       [ 8,  6,  7],\n",
       "       [ 7,  5,  6]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 4, 4],\n",
       "       [3, 6, 6],\n",
       "       [6, 3, 3],\n",
       "       [6, 4, 5],\n",
       "       [8, 2, 2]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 5, 4])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.asarray([9,5,4])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.9800189070136982"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.sum(c*x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y > y0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow",
   "language": "python",
   "name": "tensorflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
